// 导入腾讯云
import COS from 'cos-js-sdk-v5'
import { v4 } from 'uuid'
export const arr2Tree = (arr, pid) => {
  const result = []
  arr.forEach(item => {
    if (item.pid === pid) {
      result.push(item)
      const son = arr2Tree(arr, item.id)
      if (son.length !== 0) {
        item.children = son
      }
    }
  })
  // console.log(result)
  return result
}
// 创建cos的实例对象
const cos = new COS({
  SecretId: 'AKIDLYYKrAE4f181f5GGySikX9Xgov1591g6',
  SecretKey: 'EH7i5D6k9JEuLXS8YicGfqlRs3yGMGxO'
})
// 基于Promise,封装一个文件上传的函数
export const uploadFile2COS = file => {
  return new Promise(function (resolve, reject) {
    cos.putObject(
      {
        Bucket: 'bj-145hm-1310624470' /* 必须 */,
        Region: 'ap-beijing' /* 存储桶所在地域，必须字段 */,
        Key: v4() /* 必须 */,
        StorageClass: 'STANDARD',
        Body: file // 上传文件对象
      },
      function (err, data) {
        if (err) return reject(err)
        resolve(data)
      }
    )
  })
}
